<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<section class="content-header">
    <h1>工时编制</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label for="query-proCode">项目编号</label>
                                    <input id="query-proCode" type="text" name="proCode" class="form-control"
                                           placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label for="query-proName">项目名称</label>
                                    <input id="query-proName" type="text" name="proName" class="form-control"
                                           placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label for="query-special">类型</label>
                                    <select id="query-special" class="form-control" name="special">
                                        <option value="" selected>请选择</option>
                                        <option th:each="type : ${specials}" th:value="${type.name}"
                                                th:text="${type.name}"></option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-part">部分</label>
                                    <input id="query-part" type="text" name="part" class="form-control"
                                           placeholder="请输入部分">
                                </div>
                                <div class="form-group">
                                    <label for="query-status">状态</label>
                                    <select id="query-status" class="form-control" name="isCommit">
                                        <option value="">请选择</option>
                                        <option value="1" selected>未提交</option>
                                        <option value="2">部分提交</option>
                                        <option value="3">已提交</option>
                                    </select>
                                </div>
                                <!--<div class="form-group">
                                    <label for="query-isCheck">审核状态</label>
                                    <select id="query-isCheck" class="form-control" name="isCheck">
                                        <option value="" selected>请选择</option>
                                        <option value="0">未审核</option>
                                        <option value="1">已审核</option>
                                    </select>
                                </div>-->
                                <!--<div class="form-group">
                                    <label for="query-createName">工艺员</label>
                                    <input id="query-createName" type="text" name="createName" class="form-control"
                                           placeholder="请输入工艺员">
                                </div>
                                <div class="form-group">
                                    <label for="query-branchName">工艺科室</label>
                                    <input id="query-branchName" type="text" name="branchName" class="form-control"
                                           placeholder="请输入工艺科室">
                                </div>-->
                                <div class="form-group">
                                    <label for="query-startTime">日期</label>
                                    <input id="query-startTime" type="text" class="form-control datepicker" name="startTime"
                                           readonly="readonly"/>
                                    <label for="query-endTime"> 至 </label>
                                    <input id="query-endTime" type="text" class="form-control datepicker" name="endTime"
                                           readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search"
                                        onclick="js.table.search(table)">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>

                    <form id="queryForm2" onsubmit="return false;">
                        <div style="display: none">
                            <div class="form-group">
                                <label for="query2-proCode">项目编号</label>
                                <input id="query2-proCode" type="text" name="proCode" class="form-control">
                            </div>
                            <div class="form-group">
                                <label for="query2-special">类型</label>
                                <select id="query2-special" class="form-control" name="special">
                                    <option value="" selected>请选择</option>
                                    <option th:each="type : ${specials}" th:value="${type.name}"
                                            th:text="${type.name}"></option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="query2-part">部分</label>
                                <input id="query2-part" type="text" name="part" class="form-control">
                            </div>
                        </div>
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label for="query2-isQuota">定额状态</label>
                                    <select id="query2-isQuota" class="form-control" name="isQuota">
                                        <option value="" selected>全部</option>
                                        <option value="0">未定额</option>
                                        <option value="1">已定额</option>
                                        <option value="2">不需要</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query2-drawNo">图号</label>
                                    <input id="query2-drawNo" type="text" name="drawNo" class="form-control">
                                </div>
                                <button type="button" class="btn btn-success"
                                        onclick="js.table.search(table2)">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>
                        </div>
                    </form>
                    <table id="bootstrap-table2"></table>

                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    let table, table2;

    function refreshTable() {
        js.table.refresh(table2);
    }

    function show(row) {
        $("#query2-proCode").val(row.proCode);
        $("#query2-special").val(row.special);
        $("#query2-part").val(row.part);
        js.table.search(table2);
    }

    /** 提交 */
    function commit(id) {
        js.post({
            url: ctx + "est/worktime/preCommit",
            data: {
                id: id
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.confirm("确认提交吗？", function () {
                        js.post({
                            url: ctx + "est/worktime/commit",
                            data: {id: id},
                            success: function (result) {
                                if (result.type === web_status.SUCCESS) {
                                    js.modal.success(result.msg);
                                    js.table.refresh(table);
                                    js.table.refresh(table2);
                                } else {
                                    js.modal.warning(result.msg);
                                }
                            }
                        })
                    })
                } else {
                    js.modal.alert("此项目部分下，还有未定额的工艺卡，不能进行提交操作。");
                }
            }
        })
    }

    /** 取消提交 */
    function cancel(id) {
        js.modal.confirm("确认取消提交吗？", function () {
            js.post({
                url: ctx + "est/worktime/cancelCommit",
                data: {id: id},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.table.refresh(table2);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            })
        })
    }

    /** 驳回 */
    function reject(id) {
        js.modal.confirm("确认驳回吗？", function () {
            js.post({
                url: ctx + "est/worktime/reject",
                data: {id: id},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table2);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            })
        })
    }

    /** 不需要定额 */
    function need(id) {
        js.modal.confirm("此操作将清除工艺卡下所有已录入定额工时，确认不需要定额吗？", function () {
            js.post({
                url: ctx + "est/worktime/need",
                data: {id: id},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table2);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            })
        })
    }

    $(function () {

        /** 主表 */
        table = js.table.init({
            url: ctx + "est/worktime/list",
            pageSize: 5,                                       // 每页的记录行数
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "项目编号", field: 'proCode'},
                {title: "项目名称", field: 'proName',width: '260px'},
                {title: "项目数量", field: 'proNum'},
                {title: "类型", field: 'special'},
                {title: "部分", field: 'part'},
                {title: "总图号", field: 'generalDrawingNo'},
                // {title: "转定额时间", field: 'turnQuotaTime'},
                {title: "设计科室", field: 'sectionName'},
                {title: "工艺科室", field: 'deptName'},
                {title: "状态", field: 'isCommit',
                    formatter: function (value, row) {
                        if (value === 1) {
                            return '<span class="badge label-inverse">未提交</span>';
                        } else if (value === 2) {
                            return '<span class="badge label-warning">部分提交</span>';
                        } else if (value === 3) {
                            return '<span class="badge label-warning">已提交</span>';
                        }
                    }
                },
                {
                    title: "操作",
                    // width: '160px',
                    formatter: function (value, row) {
                        let actions = [];
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="show(' + js.jsonStringify(row) + ')"> 查看</a>');
                        if (row.isCommit < 3 || row.changeCommit === 0) {
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="commit(' + row.id + ')"> 提交</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 子表 */
        table2 = js.table.init({
            id: "bootstrap-table2",
            firstLoad: false,
            escape: true,
            queryForm: "queryForm2",
            url: ctx + "est/worktime/slaveList",
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "项目编号", field: 'proCodeMake'},
                {title: "项目名称", field: 'proNameMake'},
                {title: "项目数量", field: 'proNumMake'},
                {title: "类型", field: 'specialMake'},
                {title: "部分", field: 'partMake'},
                {title: "图号", field: 'drawNoMake'},
                {title: "图名", field: 'drawNameMake'},
                {title: "状态", field: 'status',
                    formatter: function (value, row) {
                        if (value === 1) {
                            return '<span class="badge label-inverse">待提交</span>';
                        } else if (value === 2) {
                            return '<span class="badge label-warning">待转定额</span>';
                        } else if (value === 3) {
                            return '<span class="badge label-warning">待提交审核</span>';
                        } else if (value === 4) {
                            return '<span class="badge label-success">已提交审核</span>';
                        } else if (value === 5) {
                            return '<span class="badge label-success">已转生产</span>';
                        }
                    }
                },
                {title: "定额状态", field: 'isQuota',
                    formatter: function (value) {
                        if (value === 0) {
                            return '<span class="badge label-default">未定额</span>';
                        } else if (value === 1) {
                            return '<span class="badge label-success">已定额</span>';
                        } else if (value === 2) {
                            return '<span class="badge label-primary">不需要</span>';
                        }
                    }
                },
                {title: "工艺科室", field: 'branchName'},
                {title: "创建人", field: 'createNameOne'},
                {title: "提报日期", field: 'createTime'},
                {
                    title: "操作",
                    width: '130px',
                    formatter: function (value, row) {
                        let actions = [];

                        if (hasP('est:worktime:edit') && row.status === 3 ) {
                            // actions.push('<a class="btn btn-warning btn-xs" target="_blank" href="' + ctx + 'est/worktime/' + row.id + '" > 定额</a>');
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="window.open(\'' + ctx + 'est/worktime/' + row.id + '\')" > 定额</a>');
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="reject(' + row.id + ')"> 驳回</a>');
                        }
                        if (row.status > 3) {
                            actions.push('<a class="btn btn-info btn-xs" target="_blank" href="' + ctx + 'est/cardList/full/' + row.id + '"> 查看</a>');
                        }
                            // actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="cancel(' + row.id + ')"> 撤消</a>');
                        return actions.join('');
                    }
                }
            ]
        });

    });
</script>
</body>
</html>
